﻿using System.Collections.ObjectModel;
using System.Windows;
using Silverlight.Training.Client.Common;
using Silverlight.Training.Client.Common.ServiceReference.Contracts;
using Silverlight.Training.Client.Common.ServiceReference.EventArgs;
using SilverLight.Training.Models;

namespace SilverLight.Training.ViewModels
{
    public class JobTitleDetailsViewModel : NotifyPropertyChangedBase
    {
        private JobTitleDetailsModel model;

        public JobTitleDetailsViewModel()
        {
            model = new JobTitleDetailsModel();
            model.GetJobTitleCollectionCompleted += model_GetJobTitleCollectionCompleted;
            model.GetJobTitleCollectionAsync();
        }

        void model_GetJobTitleCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<JobTitle> e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                    JobTitleCollection = e.Result.Data;
                else
                {
                    MessageBox.Show("No result.");
                }
            }
            else 
            {
                MessageBox.Show("Error: " + e.Error);
            }
        }

        private ObservableCollection<JobTitle> jobTitleCollection;
        public ObservableCollection<JobTitle> JobTitleCollection
        {
            get { return jobTitleCollection; }
            set
            {
                if (!ReferenceEquals(value, jobTitleCollection))
                {
                    jobTitleCollection = value;
                    RaisePropertyChanged("JobTitleCollection");
                }
            }
        }
    }
}
